package com.cola.service;

import com.cola.dao.CommonDao;
import com.cola.dao.GoodsDao;
import com.cola.entity.goods.TempGoods;
import com.cola.enums.GoodsType;
import com.cola.enums.ImportType;
import com.cola.utils.DBControl;
import org.apache.commons.dbutils.DbUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Max on 2017/8/23.
 * 商品Service
 */
public class GoodsService {
    private static volatile GoodsService instance = null;
    private static final Logger logger = LoggerFactory.getLogger(GoodsService.class);

    public static GoodsService getInstance() {
        if (instance == null) {
            synchronized (GoodsService.class) {
                if (instance == null) {
                    instance = new GoodsService();
                }
            }
        }
        return instance;
    }

    /**
     * 获取商品列表
     * @param offset 起始位置
     * @param limit 页面大小
     * @param filter 搜索条件
     * @return List<Map<String,Object>>
     * @author Max
     * @since 17/09/06
     */
    public List<Map<String,Object>> getGoodsList(Integer offset, Integer limit, String filter){
        List<Map<String,Object>> list =  GoodsDao.getInstance().getGoodsList(offset,limit,filter);
        return list;
    }

    /**
     * 获取商品数量
     * @param filter 搜索条件
     * @return Integer
     * @author Max
     * @since 17/09/06
     */
    public Integer getGoodsCount(String filter){
        return GoodsDao.getInstance().getGoodsCount(filter);
    }

    /**
     * 添加商品
     * @param goodsList 商品信息列表
     * @return true 成功 false 失败
     * @author Max
     * @since 17/09/06
     */
    public boolean addGoods(TempGoods tempGoods){
        List<Map<String,Object>> goodsList = new ArrayList<Map<String,Object>>();
        Map<String,Object> goodsMap = new HashMap<String,Object>();
        return GoodsDao.getInstance().addGoods(goodsList);
    }

    /**
     * 上架商品
     * @param goodsId 商品id
     * @param checkStatus 状态
     * @return true 成功 false 失败
     * @author Max
     * @since 17/09/06
     */
    public boolean goodsOn(Integer goodsId){
        return GoodsDao.getInstance().updateGoodsCheckStatus(goodsId, GoodsType.ON.getValue());
    }

    /**
     * 商品下架
     * @param goodsId 商品id
     * @param checkStatus 状态
     * @return true 成功 false 失败
     * @author Max
     * @since 17/09/06
     */
    public boolean goodsOff(Integer goodsId){
        return GoodsDao.getInstance().updateGoodsCheckStatus(goodsId, GoodsType.OFF.getValue());
    }

    /**
     * 商品删除
     * @param goodsId 商品id
     * @param checkStatus 状态
     * @return true 成功 false 失败
     * @author Max
     * @since 17/09/06
     */
    public boolean goodsDelete(Integer goodsId){
        return GoodsDao.getInstance().updateGoodsStatus(goodsId, GoodsType.DELETE.getValue());
    }

    /**
     * 获取商品促销信息
     * @param goodsId 商品id
     * @return Map<String,Object>促销信息
     * @author Max
     * @since 17/09/06
     */
    public Map<String,Object> getGoodsSaleInfo(Integer goodsId){
        return GoodsDao.getInstance().getGoodsSaleInfo(goodsId);
    }

    /**
     * 根据goodsId查询商品信息
     * @param  Integer goodsId
     * @return Map<String,Object>
     * @author Max
     * @since 17/09/06
     */
    public Map<String,Object> getGoodsInfoByGoodsId(Integer goodsId){
        return GoodsDao.getInstance().getGoodsInfoByGoodsId(goodsId);
    }

    /**
     * 更新商品信息
     * @param goodsInfoMap 商品信息
     * @param goodsId 商品id
     * @return true 成功 false 失败
     * @author Max
     * @since 17/09/06
     */
    public boolean updateGoodsInfoByGoodsId(Map<String,Object> goodsInfoMap,Integer goodsId){
        return GoodsDao.getInstance().updateGoodsInfoByGoodsId(goodsInfoMap,goodsId);
    }


    /**
     * 检查导入物料编号信息列表是否正确
     * @param List<List<String>> importGoodsList
     * @return Map<String,Object> 返回检查结果
     * @author Max
     * @since 2017/9/7
     */
    public Map<String,String> checkImportGoodsIsRight(List<List<String>> importGoodsList){
        Map<String,String> resultMep = new HashMap<String,String>();
        Map<String,Object> tempGoodNoMap = new HashMap<String,Object>();
        Map<String,Object> tempGoodNameMap = new HashMap<String,Object>();
        for(List<String> list : importGoodsList){
            //检查物料编号是否有重复
            if(!tempGoodNoMap.containsKey(list.get(0))){
                tempGoodNoMap.put(list.get(0),0);
            }else{
                resultMep.put("result","error");
                resultMep.put("msg","Excel表格中物料编号 "+list.get(0)+" 重复！");
                return resultMep;
            }
            //检查物料名称是否有重复
            if(!tempGoodNameMap.containsKey(list.get(1))){
                tempGoodNameMap.put(list.get(1),0);
            }else{
                resultMep.put("result","error");
                resultMep.put("msg","Excel表格中物料名称 "+list.get(1)+" 重复！");
                return resultMep;
            }
        }
        resultMep.put("result","success");
        return resultMep;
    }



    /**
     * 导入物料编号信息
     * @param fileUrl 文件Url
     * @return  Map<String,Integer> 导入返回结果
     * @author Max
     * @since 2017/9/6
     */
    public Map<String,Object> importGoodsInfo(List<List<String>> importGoodsList,String fileUrl,String username,String filename,Long userId){
        Integer count =0;
        Integer importStatus =-1;
        Map<String,Object> recordMap = new HashMap<String,Object>();
        Connection conn = null;
        recordMap.put("count",0);
        try{
            conn = DBControl.getConnection();
            conn.setAutoCommit(false);
            Map<String,Object> goodsNoMap = GoodsDao.getInstance().getGoodsNoMap();
            for (List<String> list : importGoodsList){
                if(goodsNoMap.containsKey(list.get(0))){
                    GoodsDao.getInstance().addOrUpdateGoodsInfo( conn, true, list.get(0), list.get(1)
                            , list.get(2), list.get(3), list.get(4), list.get(5));
                }else{
                    GoodsDao.getInstance().addOrUpdateGoodsInfo( conn, false, list.get(0), list.get(1)
                            , list.get(2), list.get(3), list.get(4), list.get(5));
                }
            }
            conn.commit();
            importStatus =1;
            recordMap.put("result","success");
            return recordMap;
        }catch (Exception e){
            recordMap.put("result","error");
            importStatus =-1;
            logger.error("导入物料编号信息异常！",e);
            if(conn!=null){
                try{
                    conn.rollback();
                }catch (Exception w){
                    logger.error("导入物料编号信息回滚异常！",e);
                }
            }
        }finally {
            DbUtils.closeQuietly(conn);
            CommonDao.getInstance().addImportLog(userId, ImportType.MEP_IMPORT.getDesc(),filename,fileUrl,username,
                    count,-1,-1,importStatus,"");
        }
        return null;
    }

    //==========================================赠品=============================================
    /**
     * 获取赠品列表
     * @param offset 起始位置
     * @param limit 页面大小
     * @param filter 搜索条件
     * @return List<Map<String,Object>>
     * @author Max
     * @since 17/09/06
     */
    public List<Map<String,Object>> getGiveawayList(Integer offset, Integer limit, String filter){
        return GoodsDao.getInstance().getGiveawayList(offset,limit,filter);
    }

    /**
     * 获取赠品列表
     * @return List<Map<String,Object>>
     * @author Max
     * @since 17/09/06
     */
    public List<Map<String,Object>> getAllGiveawayList(){
        return GoodsDao.getInstance().getAllGiveawayList();
    }

    /**
     * 获取赠品数量
     * @param String filter
     * @return Integer
     * @author Max
     * @since 17/09/06
     */
    public Integer getGiveawayCount(String filter){
        return GoodsDao.getInstance().getGiveawayCount(filter);
    }

    /**
     * 添加赠品
     * @param giveawayMap
     * @return true 成功 false 失败
     * @author Max
     * @since 17/09/06
     */
    public boolean addGiveaway(Map<String,Object> giveawayMap){
        return GoodsDao.getInstance().addGiveaway(giveawayMap);
    }

    /**
     * 赠品上架
     * @param giveawayId
     * @return true 成功 false 失败
     * @author Max
     * @since 17/09/06
     */
    public boolean giveawayOn(Integer giveawayId){
        return GoodsDao.getInstance().updateGiveawayStatus(giveawayId, GoodsType.ON.getValue());
    }

    /**
     * 赠品下架
     * @param giveawayId
     * @return true 成功 false 失败
     * @author Max
     * @since 17/09/06
     */
    public boolean giveawayOff(Integer giveawayId){
        return GoodsDao.getInstance().updateGiveawayStatus(giveawayId, GoodsType.OFF.getValue());
    }

    /**
     * 赠品删除
     * @param giveawayId
     * @return true 成功 false 失败
     * @author Max
     * @since 17/09/06
     */
    public boolean giveawayDelete(Integer giveawayId){
        return GoodsDao.getInstance().updateGiveawayStatus(giveawayId, GoodsType.DELETE.getValue());
    }


}
